package com.w2a.test;

import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * AES加密工具  模式:CBC  补码方式:PKCS5Padding
 */
public class AESHelper {

    private static final String KEY = "fnxpPak7SSaZZbH6gZcWIaP6NdeDer70";//密钥key(32位数)
    private static final String IV = "7ihraf0qBzTbNm70";//向量iv(16位数)

    public static String encryptString(String inputStr) {
        return encryptString(inputStr, null, null);
    }

    public static String encryptString(String inputStr, String key, String iv) {
        try {
            if (inputStr == null) return null;
            if (key == null) key = KEY;
            if (iv == null) iv = IV;
            byte[] keyBytes = key.getBytes("UTF-8");
            byte[] ivBytes = iv.getBytes("UTF-8");
            SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
            byte[] encryptedBytes = cipher.doFinal(inputStr.getBytes("UTF-8"));
            return Base64.getEncoder().encodeToString(encryptedBytes);
        } catch (Exception e) {
            return null;
        }
    }

    public static String decryptString(String inputStr) {
        return decryptString(inputStr, null, null);
    }

    public static String decryptString(String inputStr, String key, String iv) {
        try {
            if (inputStr == null) return null;
            if (key == null) key = KEY;
            if (iv == null) iv = IV;
            byte[] keyBytes = key.getBytes("UTF-8");
            byte[] ivBytes = iv.getBytes("UTF-8");
            SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
            byte[] encryptedBytes = Base64.getDecoder().decode(inputStr);
            byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
            return new String(decryptedBytes, "UTF-8");
        } catch (Exception e) {
            return null;
        }
    }

    public static byte[] encryptBytes(byte[] inputData) {
        return encryptBytes(inputData, null, null);
    }

    public static byte[] encryptBytes(byte[] inputData, String key, String iv) {
        try {
            if (inputData == null) return null;
            if (key == null) key = KEY;
            if (iv == null) iv = IV;
            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes("UTF-8"));
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
            return cipher.doFinal(inputData);
        } catch (Exception e) {
            return null;
        }
    }

    public static byte[] decryptBytes(byte[] inputData) {
        return decryptBytes(inputData, null, null);
    }

    public static byte[] decryptBytes(byte[] inputData, String key, String iv) {
        try {
            if (inputData == null) return null;
            if (key == null) key = KEY;
            if (iv == null) iv = IV;
            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes("UTF-8"));
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
            return cipher.doFinal(inputData);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
